package org.nbict.iot.protocol.util;

import com.greenpineyu.fel.FelEngine;
import com.greenpineyu.fel.context.MapContext;
import org.nbict.iot.protocol.ExpressionEngine;
import org.nbict.iot.protocol.type.ProtocolField;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * 单位换算器
 * Created by songseven on 18/6/12.
 */
public class ScalingUnit {

    private static final FelEngine engine = ExpressionEngine.ENGINE.getInstance();

    //换算
    public static BigDecimal scale(ProtocolField field, BigInteger value) {
        MapContext ctx = new MapContext();
        ctx.set(field.getName(), value);
        BigDecimal res = new BigDecimal(engine.eval(field.getUnit(), ctx).toString());
        return res;
    }

//    public static BigInteger scale(ProtocolField field, BigInteger value) {
//        MapContext ctx = new MapContext();
//        ctx.set(field.getName(), value);
//        BigInteger res = (BigInteger) engine.eval(field.getUnit(), ctx);
//        return res;
//    }

//    public static void main(String[] args) {
//
//        FelEngine fel = new FelEngineImpl();
//        String express = "num*0.1 + 100";
//        Long start = System.currentTimeMillis();
//        for (int i = 0; i < 100000; i++){
////            Bindings b = engine.createBindings();
////            b.put("num", i);
////            try {
////                System.out.println(i+ " ==> " + engine.eval(express, b));
////            } catch (ScriptException e) {
////            }
//            fel.getContext().set("num", i);
////            Map vars = new HashMap<>();
//            System.out.println(i+ " ==> " + fel.eval(express));
//        }
//        Long end = System.currentTimeMillis();
//
//        System.out.println(end - start);
//    }

    public static void main(String[] args) {

        Integer a = (new BigDecimal(1.01)).intValue();
        System.out.println(a);
    }
}
